<?php
/**
 * User: fu
 * Date: 12-3-17
 * Time: 上午12:40
 */
class Page
{
    protected  $total;
    public   $pagesize;
    protected  $curpage;
    public $linkarr=array();
    public function __construct($total,$pagesize,$curpage){
        $this->total=$total;
        $this->pagesize=$pagesize;
        $this->curpage=$curpage;
    }
    public function gettotalpage(){
        return ceil($this->total/$this->pagesize);
    }
    public function setcurpage($page){
        $this->curpage=$page;
    }
    public function getcurpage(){
        $totalpage=$this->gettotalpage();
        $curpage=$this->curpage;

        $curpage=$curpage>$totalpage?$totalpage:$curpage;

        $curpage=$curpage<1?1:$curpage;


        return $curpage;
    }
    public function getprevlink($linktpl,$countpage){
        $prevpage=$countpage-1;
        if($prevpage==0){
            return '';
        }
        return  sprintf($linktpl,$prevpage,$prevpage);
    }
    public function getnextlink($linktpl,$countpage){
        $nextpage=$countpage + 1;
        if($nextpage==$this->gettotalpage()){
            return '';
        }
        return  sprintf($linktpl,$nextpage,$nextpage);
    }
    public function getlink($pagenum,$linktpl,$pagereg,$curtpl='<span>%s</span>'){
        //所有的链接？
        $link='';

        $totalpage=$this->gettotalpage();
        if($totalpage==0){
            return $link;
        }
        $curpage= $this->getcurpage();
        $pagebasearr=array($curpage);
        $pagenum=$totalpage>$pagenum?$pagenum:$totalpage;

        for($i=1;$i<=floor($pagenum/2);$i++){
            array_unshift($pagebasearr,$curpage-$i);
            array_push($pagebasearr,$curpage+$i);
        }
        //对于有问题的？
        $pagearr=$pagebasearr;
        //小于0的？
        $basearrlen=count($pagebasearr)-1;
        foreach (array_reverse($pagebasearr) as $val) {
            $nowpage=$pagebasearr[$basearrlen]-$val+1;
            if($val<=0){
                if($nowpage<=$totalpage){
                    array_push($pagearr,$nowpage);
                }

                array_shift($pagearr);
            }
        }
        //大于总数的？
        foreach ($pagebasearr as $val) {
            $nowpage=$pagebasearr[0]-($val-$totalpage);
            if($val>$totalpage){
                if($nowpage>0){
                    array_unshift($pagearr,$nowpage);
                }
                array_pop($pagearr);
            }
        }
        //去掉 page =? ？
       // if(preg_match('/'.$pagereg.'/',$linktpl)){
            $linktpl=preg_replace('/'.$pagereg.'\d+/','',$linktpl);
      //  }
        //改变 page=?
//        $linktpl=preg_replace_callback($pagereg,function($match){
//            echo $match;
//        },$linktpl);

        foreach ($pagearr as $val) {
            //第一页去掉page ?

            if($val==1){
                $tmplink=$this->genlink(preg_replace('/'.$pagereg.'%d/','',$linktpl),$val);
            } else{
                $tmplink=$this->genlink($linktpl,$val);
            }
            array_push($this->linkarr,$tmplink);
            if($val==$curpage){
                $tmplink=$this->genlink($curtpl,$val);
            }

            $link.=$tmplink;

        }
        return $link;

    }
    protected  function genlink($linktpl,$countpage){
        return  sprintf($linktpl,$countpage,$countpage);
    }
}
